home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / FromTheMag / JW FLV MEDIA PLAYER 4.2 / mediaplayer.exe / player.swf / scripts / com / jeroenwijering / player / Controller.as next >
Text File  |  2008-11-04  |  12KB  |  386 lines

  1. package com.jeroenwijering.player
  2. {
  3.    import com.jeroenwijering.events.ControllerEvent;
  4.    import com.jeroenwijering.events.ModelEvent;
  5.    import com.jeroenwijering.events.ModelStates;
  6.    import com.jeroenwijering.events.ViewEvent;
  7.    import com.jeroenwijering.utils.Playlister;
  8.    import com.jeroenwijering.utils.Randomizer;
  9.    import flash.display.MovieClip;
  10.    import flash.events.ErrorEvent;
  11.    import flash.events.Event;
  12.    import flash.events.EventDispatcher;
  13.    import flash.geom.Rectangle;
  14.    import flash.net.URLRequest;
  15.    import flash.net.navigateToURL;
  16.    import flash.system.Capabilities;
  17.    
  18.    public class Controller extends EventDispatcher
  19.    {
  20.        
  21.       
  22.       private var config:Object;
  23.       
  24.       private var randomizer:Randomizer;
  25.       
  26.       private var view:View;
  27.       
  28.       private var model:Model;
  29.       
  30.       private var skin:MovieClip;
  31.       
  32.       private var playlister:Playlister;
  33.       
  34.       public function Controller(param1:Object, param2:MovieClip)
  35.       {
  36.          super();
  37.          config = param1;
  38.          skin = param2;
  39.          playlister = new Playlister();
  40.          playlister.addEventListener(Event.COMPLETE,playlistHandler);
  41.          playlister.addEventListener(ErrorEvent.ERROR,errorHandler);
  42.          config["controlbarsize"] = skin.controlbar.height;
  43.          redrawHandler();
  44.       }
  45.       
  46.       private function metaHandler(param1:ModelEvent) : void
  47.       {
  48.          var _loc2_:Number = NaN;
  49.          if(param1.data.duration)
  50.          {
  51.             _loc2_ = Math.round(param1.data.duration * 10) / 10;
  52.             playlister.update(config["item"],"duration",_loc2_);
  53.          }
  54.       }
  55.       
  56.       private function loadHandler(param1:ViewEvent) : void
  57.       {
  58.          var evt:ViewEvent = param1;
  59.          stopHandler();
  60.          try
  61.          {
  62.             playlister.load(evt.data.object);
  63.          }
  64.          catch(err:Error)
  65.          {
  66.             dispatchEvent(new ControllerEvent(ControllerEvent.ERROR,{"message":err.message}));
  67.          }
  68.       }
  69.       
  70.       private function volumeHandler(param1:ViewEvent) : void
  71.       {
  72.          var _loc2_:Number = NaN;
  73.          _loc2_ = param1.data.percentage;
  74.          if(_loc2_ < 1)
  75.          {
  76.             muteHandler(new ViewEvent(ViewEvent.MUTE,{"state":true}));
  77.          }
  78.          else if(!isNaN(_loc2_) && _loc2_ < 101)
  79.          {
  80.             if(config["mute"] == true)
  81.             {
  82.                muteHandler(new ViewEvent(ViewEvent.MUTE,{"state":false}));
  83.             }
  84.             config["volume"] = _loc2_;
  85.             dispatchEvent(new ControllerEvent(ControllerEvent.VOLUME,{"percentage":_loc2_}));
  86.          }
  87.       }
  88.       
  89.       private function redrawHandler(param1:ViewEvent = null) : void
  90.       {
  91.          var dat:Object = null;
  92.          var dps:String = null;
  93.          var evt:ViewEvent = param1;
  94.          dat = new Object();
  95.          try
  96.          {
  97.             dps = skin.stage["displayState"];
  98.          }
  99.          catch(err:Error)
  100.          {
  101.          }
  102.          if(dps == "fullScreen")
  103.          {
  104.             dat.fullscreen = true;
  105.             dat.width = skin.stage.stageWidth;
  106.             dat.height = skin.stage.stageHeight;
  107.          }
  108.          else if(config["resizing"])
  109.          {
  110.             dat.fullscreen = false;
  111.             dat.width = skin.stage.stageWidth;
  112.             dat.height = skin.stage.stageHeight;
  113.             if(config["controlbar"] == "bottom")
  114.             {
  115.                dat.height -= config["controlbarsize"];
  116.             }
  117.             if(config["playlist"] == "right")
  118.             {
  119.                dat.width -= config["playlistsize"];
  120.             }
  121.             else if(config["playlist"] == "bottom")
  122.             {
  123.                dat.height -= config["playlistsize"];
  124.             }
  125.          }
  126.          else
  127.          {
  128.             dat.fullscreen = false;
  129.             dat.width = config["width"];
  130.             dat.height = config["height"];
  131.          }
  132.          config["width"] = dat.width;
  133.          config["height"] = dat.height;
  134.          dispatchEvent(new ControllerEvent(ControllerEvent.RESIZE,dat));
  135.       }
  136.       
  137.       private function fullscreenHandler(param1:ViewEvent) : void
  138.       {
  139.          if(skin.stage["displayState"] == "fullScreen")
  140.          {
  141.             skin.stage["displayState"] = "normal";
  142.          }
  143.          else
  144.          {
  145.             fullscreenrect();
  146.             skin.stage["displayState"] = "fullScreen";
  147.          }
  148.       }
  149.       
  150.       private function nextHandler(param1:ViewEvent) : void
  151.       {
  152.          if(playlist && config["shuffle"] == true)
  153.          {
  154.             playItem(randomizer.pick());
  155.          }
  156.          else if(playlist && config["item"] == playlist.length - 1)
  157.          {
  158.             playItem(0);
  159.          }
  160.          else if(playlist)
  161.          {
  162.             playItem(config["item"] + 1);
  163.          }
  164.       }
  165.       
  166.       private function qualityHandler(param1:ViewEvent = null) : void
  167.       {
  168.          if(param1.data.state != undefined)
  169.          {
  170.             if(param1.data.state == config["quality"])
  171.             {
  172.                return;
  173.             }
  174.             config["quality"] = param1.data.state;
  175.          }
  176.          else
  177.          {
  178.             config["quality"] = !config["quality"];
  179.          }
  180.          dispatchEvent(new ControllerEvent(ControllerEvent.QUALITY,{"state":config["quality"]}));
  181.       }
  182.       
  183.       private function playlistHandler(param1:Event) : void
  184.       {
  185.          if(config["shuffle"] == true)
  186.          {
  187.             randomizer = new Randomizer(playlist.length);
  188.             config["item"] = randomizer.pick();
  189.          }
  190.          else if(config["item"] > playlist.length)
  191.          {
  192.             config["item"] = playlist.length - 1;
  193.          }
  194.          dispatchEvent(new ControllerEvent(ControllerEvent.PLAYLIST,{"playlist":playlist}));
  195.          if(config["autostart"] == true)
  196.          {
  197.             playItem();
  198.          }
  199.       }
  200.       
  201.       private function fullscreenrect() : void
  202.       {
  203.          try
  204.          {
  205.             skin.stage["fullScreenSourceRect"] = new Rectangle(skin.x,skin.y,Capabilities.screenResolutionX / 2,Capabilities.screenResolutionY / 2);
  206.          }
  207.          catch(err:Error)
  208.          {
  209.          }
  210.       }
  211.       
  212.       public function start(param1:Model, param2:View) : void
  213.       {
  214.          model = param1;
  215.          model.addEventListener(ModelEvent.META,metaHandler);
  216.          model.addEventListener(ModelEvent.TIME,metaHandler);
  217.          model.addEventListener(ModelEvent.STATE,stateHandler);
  218.          view = param2;
  219.          view.addEventListener(ViewEvent.FULLSCREEN,fullscreenHandler);
  220.          view.addEventListener(ViewEvent.ITEM,itemHandler);
  221.          view.addEventListener(ViewEvent.LINK,linkHandler);
  222.          view.addEventListener(ViewEvent.LOAD,loadHandler);
  223.          view.addEventListener(ViewEvent.MUTE,muteHandler);
  224.          view.addEventListener(ViewEvent.NEXT,nextHandler);
  225.          view.addEventListener(ViewEvent.PLAY,playHandler);
  226.          view.addEventListener(ViewEvent.PREV,prevHandler);
  227.          view.addEventListener(ViewEvent.QUALITY,qualityHandler);
  228.          view.addEventListener(ViewEvent.REDRAW,redrawHandler);
  229.          view.addEventListener(ViewEvent.SEEK,seekHandler);
  230.          view.addEventListener(ViewEvent.STOP,stopHandler);
  231.          view.addEventListener(ViewEvent.VOLUME,volumeHandler);
  232.       }
  233.       
  234.       private function prevHandler(param1:ViewEvent) : void
  235.       {
  236.          if(config["item"] == 0)
  237.          {
  238.             playItem(playlist.length - 1);
  239.          }
  240.          else
  241.          {
  242.             playItem(config["item"] - 1);
  243.          }
  244.       }
  245.       
  246.       private function playItem(param1:Number = undefined) : void
  247.       {
  248.          if(param1 > -1)
  249.          {
  250.             if(playlist[param1]["file"] == playlist[config["item"]]["file"])
  251.             {
  252.                playlist[param1]["duration"] = playlist[config["item"]]["duration"];
  253.             }
  254.             config["item"] = param1;
  255.          }
  256.          dispatchEvent(new ControllerEvent(ControllerEvent.ITEM,{"index":config["item"]}));
  257.       }
  258.       
  259.       private function stopHandler(param1:ViewEvent = undefined) : void
  260.       {
  261.          dispatchEvent(new ControllerEvent(ControllerEvent.STOP));
  262.       }
  263.       
  264.       public function get playlist() : Array
  265.       {
  266.          return playlister.playlist;
  267.       }
  268.       
  269.       private function linkHandler(param1:ViewEvent) : void
  270.       {
  271.          var _loc2_:Number = NaN;
  272.          var _loc3_:String = null;
  273.          _loc2_ = config["item"];
  274.          if(param1.data.index)
  275.          {
  276.             _loc2_ = param1.data.index;
  277.          }
  278.          _loc3_ = playlist[_loc2_]["link"];
  279.          if(_loc3_ != null)
  280.          {
  281.             navigateToURL(new URLRequest(_loc3_),config["linktarget"]);
  282.          }
  283.       }
  284.       
  285.       private function errorHandler(param1:ErrorEvent) : void
  286.       {
  287.          dispatchEvent(new ControllerEvent(ControllerEvent.ERROR,{"message":param1.text}));
  288.       }
  289.       
  290.       private function seekHandler(param1:ViewEvent) : void
  291.       {
  292.          var _loc2_:Number = NaN;
  293.          if(config["state"] != ModelStates.IDLE && playlist[config["item"]]["duration"] > 0)
  294.          {
  295.             _loc2_ = param1.data.position;
  296.             if(_loc2_ < 2)
  297.             {
  298.                _loc2_ = 0;
  299.             }
  300.             else if(_loc2_ > playlist[config["item"]]["duration"] - 2)
  301.             {
  302.                _loc2_ = playlist[config["item"]]["duration"] - 2;
  303.             }
  304.             dispatchEvent(new ControllerEvent(ControllerEvent.SEEK,{"position":_loc2_}));
  305.          }
  306.       }
  307.       
  308.       private function playHandler(param1:ViewEvent) : void
  309.       {
  310.          if(playlist)
  311.          {
  312.             if(param1.data.state != false && config["state"] == ModelStates.PAUSED)
  313.             {
  314.                dispatchEvent(new ControllerEvent(ControllerEvent.PLAY,{"state":true}));
  315.             }
  316.             else if(param1.data.state != false && config["state"] == ModelStates.COMPLETED)
  317.             {
  318.                dispatchEvent(new ControllerEvent(ControllerEvent.SEEK,{"position":playlist[config["item"]]["start"]}));
  319.             }
  320.             else if(param1.data.state != false && config["state"] == ModelStates.IDLE)
  321.             {
  322.                playItem();
  323.             }
  324.             else if(param1.data.state != true && (config["state"] == ModelStates.PLAYING || config["state"] == ModelStates.BUFFERING))
  325.             {
  326.                dispatchEvent(new ControllerEvent(ControllerEvent.PLAY,{"state":false}));
  327.             }
  328.          }
  329.       }
  330.       
  331.       private function itemHandler(param1:ViewEvent) : void
  332.       {
  333.          var _loc2_:Number = NaN;
  334.          _loc2_ = param1.data.index;
  335.          if(_loc2_ < 0)
  336.          {
  337.             playItem(0);
  338.          }
  339.          else if(_loc2_ > playlist.length - 1)
  340.          {
  341.             playItem(playlist.length - 1);
  342.          }
  343.          else if(!isNaN(_loc2_))
  344.          {
  345.             playItem(_loc2_);
  346.          }
  347.       }
  348.       
  349.       private function muteHandler(param1:ViewEvent) : void
  350.       {
  351.          if(param1.data.state)
  352.          {
  353.             if(param1.data.state == config["mute"])
  354.             {
  355.                return;
  356.             }
  357.             config["mute"] = param1.data.state;
  358.          }
  359.          else
  360.          {
  361.             config["mute"] = !config["mute"];
  362.          }
  363.          dispatchEvent(new ControllerEvent(ControllerEvent.MUTE,{"state":config["mute"]}));
  364.       }
  365.       
  366.       private function stateHandler(param1:ModelEvent) : void
  367.       {
  368.          if(param1.data.newstate == ModelStates.COMPLETED && (config["repeat"] == "always" || config["repeat"] == "list" && config["shuffle"] == true && randomizer.length > 0 || config["repeat"] == "list" && config["shuffle"] == false && config["item"] < playlist.length - 1))
  369.          {
  370.             if(config["shuffle"] == true)
  371.             {
  372.                playItem(randomizer.pick());
  373.             }
  374.             else if(config["item"] == playlist.length - 1)
  375.             {
  376.                playItem(0);
  377.             }
  378.             else
  379.             {
  380.                playItem(config["item"] + 1);
  381.             }
  382.          }
  383.       }
  384.    }
  385. }
  386.